package com.youlu.campus.admin.data.service.impl;

import com.youlu.campus.admin.data.entity.DayConversionStatistics;
import com.youlu.campus.admin.data.entity.DayFissionStatistics;
import com.youlu.campus.admin.data.service.DayAnalysisService;
import com.youlu.campus.admin.utils.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Objects;

@Slf4j
@Service
public class DayAnalysisServiceImpl implements DayAnalysisService {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public HashMap<String, DayFissionStatistics> getDayFissionStatistics(String tenantId, Date startTime, Date endTime) {
        log.info(":>>> 开始获取用户裂变统计,with tenantId:{},startTime:{},endTime:{}", tenantId, startTime, endTime);

        if (Objects.isNull(startTime)) {
            startTime = this.getDay0Time();
        }
        if (Objects.isNull(endTime)) {
            endTime = this.getDay23Time(startTime);
        }
        HashMap<String, DayFissionStatistics> result = new HashMap<>();
        Query query = new Query();
        Criteria criteria = new Criteria().andOperator(Criteria.where("statisticsTime").gte(DateTimeUtils.dateToISODate(startTime)), Criteria.where("statisticsTime").lte(DateTimeUtils.dateToISODate(endTime)));
        query.addCriteria(criteria);
        DayFissionStatistics d = mongoTemplate.findOne(query, DayFissionStatistics.class);
        if (Objects.nonNull(d)) {
            result.put("data", d);
        }
        return result;
    }

    @Override
    public HashMap<String, DayConversionStatistics> getDayConversionStatistics(String tenantId, Date startTime, Date endTime) {
        if (Objects.isNull(startTime)) {
            startTime = this.getDay0Time();
        }
        if (Objects.isNull(endTime)) {
            endTime = this.getDay23Time(startTime);
        }
        HashMap<String, DayConversionStatistics> result = new HashMap<>();
        Query query = new Query();
        Criteria criteria = new Criteria().andOperator(Criteria.where("statisticsTime").gte(DateTimeUtils.dateToISODate(startTime)), Criteria.where("statisticsTime").lte(DateTimeUtils.dateToISODate(endTime)));
        query.addCriteria(criteria);
        DayConversionStatistics d = mongoTemplate.findOne(query, DayConversionStatistics.class);
        if (Objects.nonNull(d)) {
            result.put("data", d);
        }
        return result;
    }

    /**
     * 获取当日零点
     *
     * @return
     */
    private Date getDay0Time() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date d = calendar.getTime();
        return d;
    }

    private Date getDay23Time(Date date) {
        Date d = new Date(date.getTime() + 24 * 60 * 60 * 1000L);
        return d;
    }
}
